﻿#region 命名空间

using System;
using System.IO;
using System.Text;

#endregion

namespace Sharp.Infrastructure
{
    /// <summary>
    ///     文件IO操作类
    /// </summary>
    public class FileIO
    {
        private static readonly object Lockhelper = new object();

        /// <summary>
        ///     写文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileString"></param>
        public void WriterFile(string filePath, string fileString)
        {
            WriterFile(filePath, fileString, Encoding.UTF8);
        }

        /// <summary>
        ///     写文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileString"></param>
        /// <param name="encoding"></param>
        public void WriterFile(string filePath, string fileString, Encoding encoding)
        {
            WriterFile(filePath, fileString, false, encoding);
        }

        /// <summary>
        ///     写文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileString"></param>
        /// <param name="append"></param>
        public void WriterFile(string filePath, string fileString, bool append)
        {
            WriterFile(filePath, fileString, append, Encoding.UTF8);
        }

        /// <summary>
        ///     写文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileString"></param>
        /// <param name="append"></param>
        /// <param name="encoding"></param>
        public void WriterFile(string filePath, string fileString, bool append, Encoding encoding)
        {
            string path = Path.GetDirectoryName(filePath);
            if (path != null && Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            lock (Lockhelper)
            {
                using (StreamWriter objWriter = new StreamWriter(filePath, append, encoding))
                {
                    objWriter.Write(fileString);
                    objWriter.Flush();
                }
            }
        }

        /// <summary>
        ///     读文件
        /// </summary>
        /// <param name="filePath"></param>
        public string ReadFile(string filePath)
        {
            return ReadFile(filePath, Encoding.UTF8);
        }

        /// <summary>
        ///     读文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="encoding"></param>
        public string ReadFile(string filePath, Encoding encoding)
        {
            string outPut;
            using (StreamReader fileStream = new StreamReader(filePath, encoding))
            {
                outPut = fileStream.ReadToEnd();
            }
            return outPut;
        }

        /// <summary>
        ///     复制目录及其文件往新目录
        /// </summary>
        /// <param name="src">源目录</param>
        /// <param name="dest">目标目录</param>
        public static void CopyDirectory(string src, string dest)
        {
            DirectoryInfo di = new DirectoryInfo(src);
            foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
            {
                string destName = Path.Combine(dest, fsi.Name);
                if (fsi is FileInfo)
                    File.Copy(fsi.FullName, destName);
                else
                {
                    Directory.CreateDirectory(destName);
                    CopyDirectory(fsi.FullName, destName);
                }
            }
        }

        /// <summary>
        ///     递归获取文件夹大小
        /// </summary>
        /// <param name="path">文件夹物理路径</param>
        /// <returns></returns>
        public static long GetFilesSize(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            long size = 0;
            foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
            {
                var info = fsi as FileInfo;
                if (info != null)
                {
                    size += info.Length;
                }
                else
                {
                    size += GetFilesSize(fsi.FullName);
                }
            }
            return size;
        }

        /// <summary>
        ///     转换文件大小的表达形式
        /// </summary>
        /// <param name="fileSize"></param>
        /// <returns></returns>
        public static string FileSize(double fileSize)
        {
            if (fileSize < 1024)
            {
                return string.Concat(fileSize, " B");
            }
            else if ((fileSize < 1048576)) //1048576=1024 * 1024
            {
                return string.Concat(Math.Round((fileSize/1024), 2), " KB");
            }
            else
            {
                return string.Concat(Math.Round(fileSize/1048576, 2), " MB");
            }
        }
    }
}